package com.hongchen.wechat.controller;

import com.hongchen.model.ActGame;
import com.hongchen.model.ActGamePrize;
import com.hongchen.model.ActPrize;
import com.hongchen.model.ActUser;
import com.hongchen.wechat.config.Api;
import com.hongchen.wechat.service.RichService;
import com.jfinal.weixin.sdk.api.ApiResult;

import java.util.Date;
import java.util.List;

/**
 * Created by 录康哲 on 2018/2/5.
 * 切了个瓜
 */
public class FruitsController extends Api {
    private final RichService richService=enhance(RichService.class);
    ActUser actU = new ActUser();//存储用户信息便于使用

    ApiResult apiResult;

    public void index(){
        this.redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId()+"&redirect_uri=http://"+chengDong()+".tunnel.echomod.cn/fruits/name&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect");
    }

    public void name(){
        apiResult=userdetail();
        //开关
        boolean ok=false;
        if(apiResult.getStr("openid")!=null){
            String nickName=apiResult.get("nickname");
            String headImg=apiResult.get("headimgurl");
            String tel=apiResult.get("tel");
            int sex=apiResult.getInt("sex");
            String city=apiResult.get("city");
            String openId=apiResult.get("openid");

            if(openId != null){
                actU.setWechatOpenid(openId);
            }
            if(headImg != null){
                actU.setHeadImg(headImg);
            }
            if(tel != null){
                actU.setTel(tel);
            }
            if(nickName != null){
                actU.setWechatNickname(nickName);
            }
            actU.setSex(sex);

            if(city != null){
                actU.setCity(city);
            }

            //判断用户是否已经在数据库存在
            ActUser user = richService.findByOpenid(openId);

            if(user == null){//新增
                actU.setCreateTime(new Date());
                if(actU.save()){
                    ActGame actGame=new ActGame();//表示用户准备玩游戏
                    actGame.setActUserId(actU.getId());
                    actGame.setNowTime(new Date());
                    actGame.save();
                }
            }else{//老用户
                actU.setId(user.getId());
                user = actU;
                user.setUpdateTime(new Date());
                user.update();//更新用户信息成功
            }
            setAttr("openid",openId);
        }else{
            ok=true;
        }

        //进行用户信息判断
        if(ok){
            index();
        }else{
            render("fruits.htm");
        }
    }

    /**
     * 用户中奖-筛子加1
     */
    public void award(){
        String openid=getPara("openid");
        //筛子次数+1
        richService.updateSieveChance(richService.findByOpenid(openid).getId());
    }

    /**
     * 用户中大奖-筛子加1
     */
    public void award_qaq(){
        String openid=getPara("openid");

        richService.updateSieveChanceTwo(richService.findByOpenid(openid).getId());
        Long prize = 0L;//定义用户中的奖品
        ActGame actg = richService.finds(richService.findByOpenid(openid).getId());//查询用户关联游戏表
        List<ActPrize> apList = richService.typePrize(8);//查询切瓜
        if(apList != null){
            ActPrize ap = richService.isPrize(apList);//奖品
            if(ap != null){
                ActGamePrize agp = new ActGamePrize();
                agp.setGameId(actg.getId());
                agp.setPrizeId(ap.getId());
                agp.setPrizeNum(richService.lastPrizeNo()); //中奖编号
                agp.setAwardTime(new Date());//当前中奖时间
                agp.setAcceptTime(BigWheelController.getFetureDate(7));//生成7天后的领奖截止时间
                agp.setAcceptAddres("河南省郑州市英协路56号传媒创意中心14层河南新维思广告有限公司 泰坦XV号");
                if(agp.save()){//保存中奖信息
                    if(richService.lessOnePrize(ap.getId()) >= 1){//奖品数量减一
                        prize = ap.getId();
                    }
                }
                setAttr("valuep",ap.getPrizeValue());///奖品价值
                setAttr("namep",ap.getPrizeName());///奖品名
                setAttr("nop",agp.getPrizeNum());///奖品编号
                setAttr("big",true);
                setAttr("success",true);
            }
        }
        renderJson();
    }
}
